package example;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Scanner;

import commsCPN.JavaCPN;

public class MinimalExample {

    public static void main(String[] args) throws IOException {
        boolean connected = false;
        JavaCPN jCPN = new JavaCPN();
        Scanner stdIn = new Scanner(System.in);
        String receivedString = null;
        
        while (!connected) {
            System.out.print("localhost:9000? (Y/N) ");
            if (stdIn.nextLine().equals("Y")) {
                jCPN.connect("localhost", 9000);
                connected = true;
            } else {
                break;
            }
        }
        
        while (connected) {
            System.out.println("Awaiting message..");
            receivedString = jCPN.receive().toString();
            System.out.println("Message "+ " (" + receivedString.length() + " Bytes)" + ": " + receivedString);

            System.out.print("Quit? (Y/N) ");
            if (stdIn.nextLine().equals("Y")) {
                jCPN.send(new ByteArrayInputStream("quit".toString().getBytes()));
                jCPN.disconnect();
                connected = false;
            } else {
                jCPN.send(new ByteArrayInputStream("continue".toString().getBytes()));
            }
        }
        System.out.print("Connection closed. Execution ended.");
        stdIn.close();
    }
}
